from django import forms
from django.forms import TextInput, CheckboxSelectMultiple
from . import models


class InPatientsForm(forms.ModelForm):
    #form模型
    class Meta:
        model = models.InPatientsModel
        fields = "__all__"
        widgets = {
            '干预方式': CheckboxSelectMultiple(),
            '饮食习惯': CheckboxSelectMultiple(),
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['干预方式'].widget.choices = [("TPN", "TPN"), ("SPN", "SPN"),
                                              ("EN", "EN"), ("ONS", "ONS"),
                                              ("饮食", "饮食"), ("营养教育", "营养教育")]
        self.fields['饮食习惯'].widget.choices = [("喜烫", "喜烫"), ("酒", "酒"),
                                              ("进食快", "进食快"), ("其他", "其他")]
        if kwargs.get('instance'):
            if self.initial['干预方式']:
                self.initial['干预方式'] = eval(self.initial['干预方式'])
            if self.initial['饮食习惯']:
                self.initial['饮食习惯'] = eval(self.initial['饮食习惯'])


#肠内耐受表单
class NutrtionToleranceForm(forms.ModelForm):

    class Meta:
        model = models.NutrtionToleranceModel
        fields = "__all__"
        widgets = {
            '肠内营养相关并发症': CheckboxSelectMultiple(),
            '肠外营养途径': CheckboxSelectMultiple(),
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['肠内营养相关并发症'].widget.choices = [
            ("腹泻", "腹泻"),
            ("腹胀", "腹胀"),
            ("腹痛", "腹痛"),
            ("恶心呕吐", "恶心呕吐"),
            ("误吸", "误吸"),
            ("导管阻塞或移位", "导管阻塞或移位"),
            ("脱水", "脱水"),
            ("电解质紊乱", "电解质紊乱"),
            ("高血糖", "高血糖"),
        ]
        self.fields['肠外营养途径'].widget.choices = [
            ("外周静脉", "外周静脉"),
            ("PICC", "PICC"),
            ("中心静脉", "中心静脉"),
        ]
        if kwargs.get('instance'):
            if self.initial['肠内营养相关并发症']:
                self.initial['肠内营养相关并发症'] = eval(self.initial['肠内营养相关并发症'])
            if self.initial['肠外营养途径']:
                self.initial['肠外营养途径'] = eval(self.initial['肠外营养途径'])
